package com.wzt.lianfirecontrol.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.App;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.adapter.ChooseListAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.AppVersionModel;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.user.CompanyModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.http.HttpUtil;
import com.wzt.lianfirecontrol.inter.SetTime;
import com.wzt.lianfirecontrol.view.MyListView;
import com.wzt.lianfirecontrol.view.MyPopupWindow;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DialogUtils {

    /**
     * 创建确定对话框
     * @param context
     * @param msg
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createSureAndCancelDialog(Context context, String msg, String cancelStr,
                                                   String sureStr, View.OnClickListener cancelClickListener,
                                                   View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_sure_no, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        v.findViewById(R.id.tv_title).setVisibility(View.VISIBLE);

        TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 创建确定对话框
     * @param context
     * @param msg
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createSureAndCancelDialog(Context context, String title, String msg, String cancelStr,
                                                   String sureStr, View.OnClickListener cancelClickListener,
                                                   View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_sure_no, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TextView tv_title = v.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(title);

        TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 创建列表选择弹窗
     * @param context
     * @param parentView
     * @param nameList
     * @param onItemClickListener
     * @return
     */
    public static MyPopupWindow createListPopupWindow(final Context context, ViewGroup parentView, List<String> nameList, AdapterView.OnItemClickListener onItemClickListener){
        View view = LayoutInflater.from(context).inflate(R.layout.pop_list_bottom, parentView, false);
        MyListView mlv_pop_list = (MyListView) view.findViewById(R.id.mlv_pop_list);
        PopListAdapter popListAdapter = new PopListAdapter(context, nameList);
        mlv_pop_list.setAdapter(popListAdapter);
        mlv_pop_list.setOnItemClickListener(onItemClickListener);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mlv_pop_list.getLayoutParams();
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        final MyPopupWindow popupWindow = new MyPopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                DialogUtils.darkenBackground(1f, (Activity) context);
            }
        });
        view.findViewById(R.id.rl_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        return popupWindow;
    }

    /**
     * 创建仿ios列表选择弹窗
     * @param context
     * @param parentView
     * @param nameList
     * @param onItemClickListener
     * @return
     */
    public static MyPopupWindow createListLikeIosPopupWindow(final Context context, ViewGroup parentView,
                                                             List<String> nameList,
                                                             AdapterView.OnItemClickListener onItemClickListener,
                                                             String sureStr,
                                                             View.OnClickListener onClickListener,
                                                             String currentSelectStr){
        View view = LayoutInflater.from(context).inflate(R.layout.pop_list_bottom_like_ios, parentView, false);
        MyListView mlv_pop_list = view.findViewById(R.id.mlv_pop_list);
        PopListAdapter popListAdapter = new PopListAdapter(context, nameList);
        popListAdapter.setIosList(true);
        popListAdapter.setCurrentSelectStr(currentSelectStr);
        mlv_pop_list.setAdapter(popListAdapter);
        mlv_pop_list.setOnItemClickListener(onItemClickListener);

        TextView tv_sure = view.findViewById(R.id.tv_sure);
        tv_sure.setText(sureStr);
        tv_sure.setOnClickListener(onClickListener);


        final MyPopupWindow popupWindow = new MyPopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                DialogUtils.darkenBackground(1f, (Activity) context);
            }
        });
        view.findViewById(R.id.rl_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        return popupWindow;
    }

    /**
     * 列表选择适配器
     */
    private static class PopListAdapter extends BaseAdapter {
        private Context context;
        private boolean isIosList = false;
        private String currentSelectStr;
        private List<String> strList = new ArrayList<>();

        public boolean isIosList() {
            return isIosList;
        }

        public void setIosList(boolean iosList) {
            isIosList = iosList;
        }

        public String getCurrentSelectStr() {
            return currentSelectStr;
        }

        public void setCurrentSelectStr(String currentSelectStr) {
            this.currentSelectStr = currentSelectStr;
        }

        public PopListAdapter(Context context, List<String> strList) {
            this.context = context;
            this.strList = strList;
        }

        @Override
        public int getCount() {
            return strList.size();
        }

        @Override
        public Object getItem(int position) {
            return strList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (null == convertView){
                if (isIosList) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_list_like_ios, null);
                }else {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_list, null);
                }
                viewHolder = new ViewHolder();
                viewHolder.tv_list_click = convertView.findViewById(R.id.tv_list_click);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv_list_click.setText(strList.get(position));
            if (isIosList){
                if (!StringUtils.isEmpty(currentSelectStr) && currentSelectStr.equals(strList.get(position))){
                    viewHolder.tv_list_click.setTextColor(context.getResources().getColor(R.color.select_text_color));
                }else {
                    viewHolder.tv_list_click.setTextColor(context.getResources().getColor(R.color.main_color));
                }
            }else {
                viewHolder.tv_list_click.setTextColor(context.getResources().getColor(R.color.black));
            }
            return convertView;
        }

        class ViewHolder{
            TextView tv_list_click;
        }
    }

    /**
     * 选择列表
     * @param context
     * @param list
     * @param title
     * @param position
     * @param onItemClickListener
     * @return
     */
    public static Dialog getChooseListDialog(Context context, List<BannerModel> list, String title, int position,
                                             BaseRecyclerAdapter.OnItemClickListener onItemClickListener){
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_list, null);
        TextView tv_title = view.findViewById(R.id.tv_title);
        View v_line = view.findViewById(R.id.v_line);

        RecyclerView rlv_list = view.findViewById(R.id.rlv_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_list.setLayoutManager(linearLayoutManager);
        ChooseListAdapter chooseListAdapter = new ChooseListAdapter(context);
        chooseListAdapter.setCurrentPosition(position);
        chooseListAdapter.addDatas(list);
        rlv_list.setAdapter(chooseListAdapter);
        chooseListAdapter.setOnItemClickListener(onItemClickListener);

        if (StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlv_list.getLayoutParams();
            layoutParams.topMargin = Utils.dip2px(context, 15);
        }else {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
            v_line.setVisibility(View.VISIBLE);
        }

        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(context) * 2 / 3));
        dialog.setCanceledOnTouchOutside(true);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        return dialog;
    }

    /**
     * @param time 2019年8月3号
     * @param context
     */
    public static Dialog createDatePickerDialog(final Context context, String time, final SetTime setTime){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_date_picker, null);
        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        final DatePicker datePicker = v.findViewById(R.id.dp_select_date);
        try {
            if (!StringUtils.isEmpty(time)) {
                String year = time.split("-")[0];
                String month = time.split("-")[1];
                String day = time.split("-")[2];
                if (!StringUtils.isEmpty(year) && !StringUtils.isEmpty(month) && !StringUtils.isEmpty(day)) {
                    datePicker.init(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day), null);
                }
            }
        }catch (Exception e){

        }

        TextView tv_clean = (TextView) v.findViewById(R.id.tv_clean);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
        tv_clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTime.setFilterTime(datePicker.getYear(), datePicker.getMonth()+1, datePicker.getDayOfMonth());
                dialog.dismiss();
            }
        });

        return dialog;
    }

    /** 切换单位
     * @param list
     * @param context
     */
    public static Dialog createChangeCompanyDialog(final Context context, List<CompanyModel> list){
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_change_company, null);
        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(context) * 2 / 3));

        LinearLayout ll_content = view.findViewById(R.id.ll_content);
        createChangeCompanyItem(context, ll_content, list, dialog);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        return dialog;
    }

    /**
     * 创建切换单位子布局
     * @param context
     * @param list
     * @return
     */
    private static void createChangeCompanyItem(Context context, LinearLayout parentLayout, List<CompanyModel> list, Dialog dialog){
        for (CompanyModel companyModel:list){
            companyModel.setItemShow(true);// 默认都展开

            View view = LayoutInflater.from(context).inflate(R.layout.item_change_company, parentLayout, false);
            LinearLayout ll_parent_content = view.findViewById(R.id.ll_parent_content);
            TextView tv_company_name = view.findViewById(R.id.tv_company_name);
            ImageView iv_expand = view.findViewById(R.id.iv_expand);
            View v_bottom_line = view.findViewById(R.id.v_bottom_line);
            LinearLayout ll_child_content = view.findViewById(R.id.ll_child_content);

            if (UserInfoModel.getCompandId(context).equals(companyModel.getId())){
                ll_parent_content.setBackground(context.getResources().getDrawable(R.drawable.shape_main_color_tran_bg_frame));
                v_bottom_line.setVisibility(View.GONE);
            }else {
                ll_parent_content.setBackgroundColor(context.getResources().getColor(R.color.white));
                v_bottom_line.setVisibility(View.VISIBLE);
            }
            // 是否为子公司
            if (StringUtils.isEmpty(companyModel.getParentId())){
                tv_company_name.setTextSize(14);
            }else {
                tv_company_name.setTextSize(13);
            }
            tv_company_name.setText(companyModel.getName());
            tv_company_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 选中单位
                    closeDialog(dialog);
                    UserInfoModel.setCompandName(context, companyModel.getName());
                    UserInfoModel.setCompandId(context, companyModel.getId());
                }
            });
            if (null != companyModel.getChildren() && companyModel.getChildren().size() > 0){
                iv_expand.setVisibility(View.VISIBLE);
                if (companyModel.isItemShow()){
                    iv_expand.setImageResource(R.mipmap.up_expand);
                    ll_child_content.setVisibility(View.VISIBLE);
                }else {
                    iv_expand.setImageResource(R.mipmap.down_expand);
                    ll_child_content.setVisibility(View.GONE);
                }
                iv_expand.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 缩放
                        if (companyModel.isItemShow()){
                            ll_child_content.setVisibility(View.GONE);
                            iv_expand.setImageResource(R.mipmap.down_expand);
                        }else {
                            ll_child_content.setVisibility(View.VISIBLE);
                            iv_expand.setImageResource(R.mipmap.up_expand);
                        }
                        companyModel.setItemShow(!companyModel.isItemShow());
                    }
                });
                createChangeCompanyItem(context, ll_child_content, companyModel.getChildren(), dialog);
            }else {
                ll_child_content.setVisibility(View.GONE);
                iv_expand.setVisibility(View.INVISIBLE);
            }
            parentLayout.addView(view);
        }
    }

    /**
     * 创建设备巡查完成对话框
     * @param context
     * @param msg
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createNfcDeviceCompleteDialog(Context context, String msg, String cancelStr,
                                                       String sureStr, View.OnClickListener cancelClickListener,
                                                       View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_nfc_complete, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        v.findViewById(R.id.tv_title).setVisibility(View.GONE);

        TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * app版本更新弹窗
     * @param context
     * @param appVersionModel
     * @return
     */
    public static Dialog createAppUpdateDialog(Context context, AppVersionModel appVersionModel){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_app_update, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TextView tv_app_update_message = v.findViewById(R.id.tv_app_update_message);
        LinearLayout ll_update_btn = v.findViewById(R.id.ll_update_btn);
        TextView tv_update_btn = v.findViewById(R.id.tv_update_btn);
        TextView tv_no_update_btn = v.findViewById(R.id.tv_no_update_btn);

        LinearLayout ll_pb = v.findViewById(R.id.ll_pb);
        ll_pb.setVisibility(View.GONE);
        ProgressBar pb_update = v.findViewById(R.id.pb_update);
        pb_update.setMax(100);
        pb_update.setProgress(0);
        TextView tv_progress = v.findViewById(R.id.tv_progress);
        tv_progress.setText("0%");

        tv_app_update_message.setText(appVersionModel.getRemark());

        View.OnClickListener autoUpdateClickListener = new View.OnClickListener() {
            Dialog netAndWifiDialog;
            @Override
            public void onClick(View v) {
                if (v.getTag() != null && v.getTag().getClass() == String.class) {
                    FileUtil.install(String.valueOf(v.getTag()), context);
                    return;
                }
                if (HttpUtil.isNetworkAvailable(context)) {
                    if (!HttpUtil.isWifiDataEnable(context)) {
                        netAndWifiDialog = createSureAndCancelDialog(context, "当前为移动网络，是否继续升级",
                                "取消", "继续", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //取消下载
                                        closeDialog(netAndWifiDialog);
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        updateAppAction(context, dialog, ll_update_btn, tv_update_btn, ll_pb, pb_update, tv_progress, appVersionModel);
                                        closeDialog(netAndWifiDialog);
                                    }
                                });
                        showDialog(netAndWifiDialog);
                    } else {
                        updateAppAction(context, dialog, ll_update_btn, tv_update_btn, ll_pb, pb_update, tv_progress, appVersionModel);
                    }
                } else {
                    Toast.makeText(context, context.getString(R.string.no_net), Toast.LENGTH_LONG).show();
                }
            }
        };
        tv_update_btn.setOnClickListener(autoUpdateClickListener);

        if ("0".equals(appVersionModel.getForceUpdate())){
            // 不强制更新
            tv_no_update_btn.setVisibility(View.VISIBLE);
            tv_no_update_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleCache.saveDataForOneDay(context, ConstData.UPDATE_DIALOG_SHOW, "1");
                    dialog.dismiss();
                }
            });
        }else {
            tv_no_update_btn.setVisibility(View.GONE);
        }

        return dialog;
    }

    /**
     * 更新动作
     * @param appVersionModel
     */
    private static void updateAppAction(Context context, Dialog dialog, LinearLayout ll_update_btn, TextView tv_update_btn,
                                        LinearLayout ll_pb, ProgressBar pb_update, TextView tv_progress, AppVersionModel appVersionModel){
        ll_update_btn.setVisibility(View.GONE);
        ll_pb.setVisibility(View.VISIBLE);
        pb_update.setMax(100);

        RequestParams params = new RequestParams(appVersionModel.getApkDownloadUrl());
        params.setSaveFilePath(ConstData.getMyAppDownloadPath(App.getApp(), appVersionModel.getVersion()));
        x.http().get(params, new Callback.ProgressCallback<File>() {
            @Override
            public void onSuccess(File file) {
                if (file == null || file.length() == 0) {
                    ToastUtils.showToast(context, "下载文件错误");
                    return;
                }
               // if (Utils.getVersionCode(App.getApp()) < info.getAppVersionCode()) {
                    ll_update_btn.setVisibility(View.VISIBLE);
                    tv_update_btn.setVisibility(View.VISIBLE);
                    tv_update_btn.setTag(file.getAbsolutePath());
                    tv_update_btn.setText("立即安装");
                    ll_pb.setVisibility(View.GONE);
                    FileUtil.install(file.getPath(), context);
//                } else {
//                    Toast.makeText(context, "下载文件错误", Toast.LENGTH_LONG).show();
//                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.showToast(context, ex.getMessage());
                dialog.dismiss();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
            //网络请求之前回调
            @Override
            public void onWaiting() {
            }
            //网络请求开始的时候回调
            @Override
            public void onStarted() {
            }
            //下载的时候不断回调的方法
            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                long apkSize = total;
                try {
                    apkSize = Long.parseLong(appVersionModel.getApkSize());
                }catch (Exception e){}
                int cPro = (int) (1.0 * current / apkSize * 100);
                tv_progress.setText(cPro + "%");
                pb_update.setProgress(cPro);
            }
        });
    }

    /**
     * 改变背景颜色
     */
    public static void darkenBackground(Float bgcolor, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
    }

    public static void closeDialog(Dialog dialog) {
        if (null != dialog && dialog.isShowing()) {
            dialog.cancel();
        }
    }

    public static void showDialog(Dialog dialog) {
        try {
            if (null != dialog && null != dialog.getContext() && null != dialog.getWindow()) {
                dialog.show();
            }
        }catch (Exception e){}
    }

    public static void closePopUpWindow(PopupWindow window) {
        if (null != window && window.isShowing()) {
            window.dismiss();
        }
    }

}
